package com.cool4j.generator.builder

import com.cool4j.generator.config.GeneratorConstants
import com.cool4j.generator.entity.Column
import com.cool4j.generator.entity.Table
import com.cool4j.generator.exception.ExceptionMessage
import com.cool4j.generator.util.GeneratorUtil
import com.cool4j.generator.util.LogUtil
import com.cool4j.view.entity.HandlerVO
import freemarker.template.Configuration
import freemarker.template.TemplateException
import org.apache.commons.lang3.StringUtils

/**
 * 列表页面构造器类
 *
 * @author Frank create on 16/1/8 15:12
 */
class ListPageBuilder extends BaseBuilder {

    @Override
    boolean builder(Table table, String packagePath, HandlerVO handlerVO, Configuration config) {
        try {
            /* 获取或创建一个模板 */
            def template = config.getTemplate("ListPage.ftl", "UTF-8")
            def dataMap = getDataMap(table, packagePath, handlerVO)
            dataMap.put("paramColumns", getParamColumns(table.getColumnList()))
            def filePath = handlerVO.getOutputPath() + "/view/" + table.getClassName() + "List.ftl"
            GeneratorUtil.writeFile(filePath, template, dataMap)
        } catch (IOException e) {
            LogUtil.logging(ExceptionMessage.LOG_LIST_PAGE_BUILDER_IO_EXCEPTION, e)
            return false
        } catch (TemplateException e) {
            LogUtil.logging(ExceptionMessage.LOG_LIST_PAGE_BUILDER_TEMPLATE_EXCEPTION, e)
            return false
        } catch (Exception e) {
            LogUtil.logging(ExceptionMessage.LOG_LIST_PAGE_BUILDER_OTHER_EXCEPTION, e)
            return false
        }
        return true
    }

    private def getParamColumns(columns) {
        def paramColumns = []
        columns.each { column ->
            if (StringUtils.contains(GeneratorConstants.PARAM_TYPE, column.getFullType())) {
                def paramColumnStart = new Column()
                paramColumnStart.setColumnName(column.getColumnName() + GeneratorConstants.TIME_PREFIX)
                paramColumnStart.setColumnSize(column.getColumnSize())
                paramColumnStart.setIsNullable(column.getIsNullable)
                paramColumnStart.setIsPrimary(column.getIsPrimary)
                paramColumnStart.setRemark(column.getRemark() + "开始时间")
                paramColumnStart.setTypeName(column.getTypeName())
                paramColumns << paramColumnStart

                def paramColumnEnd = new Column()
                paramColumnEnd.setColumnName(column.getColumnName() + GeneratorConstants.TIME_PREFIX)
                paramColumnEnd.setColumnSize(column.getColumnSize())
                paramColumnEnd.setIsNullable(column.getIsNullable())
                paramColumnEnd.setIsPrimary(column.getIsPrimary())
                paramColumnEnd.setRemark(column.getRemark() + "结束时间")
                paramColumnEnd.setTypeName(column.getTypeName())
                paramColumns << paramColumnEnd
            } else {
                paramColumns << column
            }
        }
        return paramColumns
    }

}
